<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>weakSet的使用</title>
</head>
<body>
  
  <script>
    // 1.Weak Reference(弱引用)和Strong Reference(强引用)
    let obj1 = { name: "why" }
    let obj2 = { name: "kobe" }
    let obj3 = { name: "jame" }

    // let arr = [obj1, obj2, obj3]
    // obj1 = null
    // obj2 = null
    // obj3 = null

    // const set = new Set(arr)
    // arr = null

    // 2.WeakSet的用法
    // 2.1.和Set的区别一: 只能存放对象类型
    const weakset = new WeakSet()
    weakset.add(obj1)
    weakset.add(obj2)
    weakset.add(obj3)
    console.log(weakset)
    // 2.2.和Set的区别二: 对对象的引用都是弱引用

    // 3.WeakSet的应用
    const pWeakSet = new WeakSet()
    
    class Person {
      constructor() {
        pWeakSet.add(this)
      }
      running() {
        if (!pWeakSet.has(this)) {
          console.log("Type error: 调用的方式不对")
          return
        }
        console.log("running")
      }
    }

    let p = new Person()
    p.running()
    const runFn = p.running
    runFn()
    const obj = {run: runFn}
    obj.run()
  </script>
</body>
</html>